package com.ys.多线程.p2中断线程;

/**
 * 描述:
 *
 * @author CodeYang
 * @version 2021/1/5 19:13
 */
public class P2自定义线程标志判断是否继续执行线程  {

    public static void main(String[] args) throws InterruptedException {
        MyInterrupt myInterrupt = new MyInterrupt();
        Thread thread = new Thread(myInterrupt);
        // 启动线程
        thread.start();
        thread.sleep(5000);
        // 中断线程
        myInterrupt.setFlage(false);

    }
}

class  MyInterrupt implements  Runnable{

    private   boolean flage = true;



    /**
     * 利用set方法来停止线程执行
     * @param flage
     */
    public void setFlage(boolean flage) {
        this.flage = flage;
    }

    @Override
    public void run() {
        while (flage){
            System.out.println(Thread.currentThread().getName()+"启动");
        }
    }
}
